package com.fzy.leetcodeExam.test01;

import java.util.*;

public class Main {

    public static void main(String[] args) {
        Main test = new Main();
//        test.findMatrix(new int[]{1,3,4,1,2,3,1});
        System.out.printf("%d", test.findTheLongestBalancedSubstring("001"));
    }


    public int findTheLongestBalancedSubstring(String s) {

        char[] strs = s.toCharArray();
        int len = strs.length;
        int ans = 0;

        int nums_0 = 0;
        int nums_1 = 0;

        for(int i = 0;i<len;i++){
            nums_0 = 0;
            nums_1 = 0;
            for(int k = i;k<len;k++){
                if(strs[k] == '0'){
                    nums_0++;
                }else {
                    int j = k;
                    while (j<len && strs[j] == '1' && nums_0 != 0){
                        nums_1++;
                        j++;
                        if(nums_0 == nums_1){
                            ans = Math.max(ans,nums_0*2);
                            nums_0 = 0;
                            nums_1 = 1;
                        }
                    }
                }
            }
        }
        return ans;
    }


    public List<List<Integer>> findMatrix(int[] nums) {

        List<List<Integer>> ans = new ArrayList<>();

        if(nums == null || nums.length == 0){
            return ans;
        }

        List<Integer> numslist = new ArrayList<>();

        for(int num:nums){
            numslist.add(num);
        }
//        int len = numslist.size();

        while (!numslist.isEmpty()){
            Set<Integer> set = new HashSet<>();
            for(int i = 0;i<numslist.size();i++){
                if(!set.contains(numslist.get(i))){
                    set.add(numslist.get(i));
                    numslist.remove(numslist.get(i));
                    i--;
                }
            }

            List<Integer> res = new ArrayList<>();
            for(int val : set){
                res.add(val);
            }
            ans.add(res);
        }

        return ans;
    }


    public int miceAndCheese(int[] reward1, int[] reward2, int k) {


        int n = reward1.length;
        if(k >= n){
            return Arrays.stream(reward1).sum();
        }
        int[] maxreward = new int[n];
        for(int i = 0;i<reward1.length;i++){

            if(reward1[i] > reward2[i]){
                maxreward[i] = reward1[i];
            }else {
                maxreward[i] = reward2[i];
            }
        }

        Arrays.sort(reward1);
        List<Integer> list = new ArrayList<>();

        for(int i = 0;i<maxreward.length;i++){
            list.add(maxreward[i]);
        }
        int sum = 0;

        for(int j = n-1;j>=n-k;j--){
            int val = reward1[j];
            if(list.contains(val)){
                list.remove(val);
            }
            sum+= val;
        }

        

        return 0;
    }

}
